package com.adcc.ags.gms.mq.controller;

import com.adcc.ags.gms.core.util.Constant;
import com.adcc.ags.gms.core.util.LogFactory;
import com.adcc.ags.gms.core.vo.ResultState;
import com.adcc.ags.gms.core.vo.ResultVO;
import com.adcc.ags.gms.mq.service.ChannelService;
import com.adcc.ags.gms.mq.vo.ChannelVO;
import com.google.common.base.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/channel", produces = MediaType.APPLICATION_JSON_VALUE)
public class ChannelController {

    @Autowired
    private LogFactory log;

    @Autowired
    private ChannelService channelService;

    @RequestMapping(value = "/findAllChannel", method = RequestMethod.GET)
    public @ResponseBody ResultVO findAllChannel() {
        ResultVO resultVO = new ResultVO();
        try {
            Optional<List<ChannelVO>> optional = channelService.findAllChannel();
            if (optional.isPresent()) {
                List<ChannelVO> list = optional.get();
                if (list.size() > 0) {
                    resultVO.getData().put("listChannel", list);
                }
            } else {
                resultVO.setCode(-1);
                resultVO.setState(ResultState.FAILURE);
                resultVO.setReason(Constant.GMS_PCF_ERROR);
            }
        } catch (Exception ex) {
            log.error(ChannelController.class, "findAllChannel() error", ex);
            resultVO.setCode(-1);
            resultVO.setState(ResultState.FAILURE);
            resultVO.setReason(Constant.GMS_SERVER_ERROR);
        }
        return resultVO;
    }

}
